package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.BusinessDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BusinessDaoImpl implements BusinessDao {
    private Connection con;
    private PreparedStatement pst;
    private ResultSet rs;
    @Override
    public List<Business> listBusinessByOrderTypeId(int orderTypeId) throws Exception {
        List<Business> list = new ArrayList<>();
        String sql = "SELECT * FROM business WHERE orderTypeId=? ORDER BY businessId";
       try{
           con = DBUtil.getConnection();
           pst = con.prepareStatement(sql);
           pst.setInt(1,orderTypeId);
           //执行查询
           rs = pst.executeQuery();
           //遍历结果集
           while (rs.next()) {
               Business business = new Business();
               business.setBusinessId(rs.getInt("businessId"));
               business.setBusinessName(rs.getString("businessName"));
               business.setBusinessAddress(rs.getString("businessAddress"));
               business.setBusinessExplain(rs.getString("businessExplain"));
               business.setBusinessImg(rs.getString("businessImg"));
               business.setOrderTypeId(rs.getInt("orderTypeId"));
               business.setStarPrice(rs.getDouble("starPrice"));
               business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
               business.setRemarks(rs.getString("remarks"));
               //将对象保存到集合中
               list.add(business);
           }
       }finally {                          //这里不能处理异常，也就是没有catch，只有finally
           DBUtil.close(rs,pst);           //这里负责关闭PreparedStatement和ResultSet
       }

        return list;
    }

    @Override
    public Business getBusinessById( int businessId) throws Exception{
        String sql = "SELECT * FROM business WHERE businessId=?";
        Business business = null;
        try{
            con = DBUtil.getConnection();//进行数据库连接
            pst = con.prepareStatement(sql);//传输数据库文件
            pst.setInt(1,businessId);
            //执行查询
            rs = pst.executeQuery();
            //遍历结果集
            while (rs.next()) {
                business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
            }
        } finally {                          //这里不能处理异常，也就是没有catch，只有finally
            DBUtil.close(rs,pst);           //这里负责关闭PreparedStatement和ResultSet
        }
        return business;
    }
}
